$(document)
		.ready(
				function() {
					var contextPath = document.location.pathname;
					var index = contextPath.substr(1).indexOf("/");
					contextPath = contextPath.substr(1, index);
					var basePath = "/" + contextPath;
					var totalCount = $("#totalCount").val();
					var pSize = $("#pageSize").val();

					var pageIndex = $("#pageIndex").val();
					var pageCount = $("#pageCount").val();

					$('#tools').delegate('#queryBtn', 'click', function() {

						var productName = $("#productName").val();
						var reType = $("#reType").val();
						// alert("reType==="+reType);
						queryStock(reType, productName);

					});

					$('form')
							.submit(
									function() {
										if (typeof jQuery.data(this,
												"disabledOnSubmit") == 'undefined') {
											jQuery.data(this,
													"disabledOnSubmit", {
														submited : true
													});
											$(
													'input[type=submit], input[type=button]',
													this).each(
													function() {
														$(this).attr(
																"disabled",
																"disabled");
													});
											return true;
										} else {
											return false;
										}
									});

					$("#protypeDetail").on('hidden.bs.modal', function() {
						$(this).removeData("bs.modal");
					});

					$("#optBtn").delegate(
							'#saveBtn',
							'click',
							function() {

								$("#productDetail").attr("method", "post");

								var proId = $("#proId").val();
								// alert(proId);
								if (proId == "" || proId == null
										|| proId == "undefined") {

									$("#productDetail").attr("action",
											"save.do");
									alert("save");
								} else {
									$("#productDetail").attr("action",
											"update.do");
									alert("update");
								}
								$("#productDetail").submit();

							});

					$("#conditions").delegate('#reTypes', 'change', function() {

						alert("fuck.." + $(this).val());
					}

					);

					$('#pagination2')
							.jqPaginator(
									{
										totalPages : pageCount,
										visiblePages : 5,
										currentPage : 1,

										prev : '<li class="prev"><a href="javascript:void(0);">上一页</a></li>',
										page : '<li class="page"><a href="javascript:void(0);">{{page}}</a></li>',
										next : '<li class="next"><a href="javascript:void(0);">下一页</a></li>',
										onPageChange : function(num, type) {
											// alert("pageIndex="+num);
											// alert("type="+type);
											$("#pageIndex").val(num);
											var typeId = $("#proTypeId").val();
											// alert("type----Id="+typeId);
											var queryUrl = "queryByPage.do";
											$
													.ajax({
														url : queryUrl,
														dataType : 'json',
														data : {
															pageIndex : num,
															pageSize : pSize,
															typeId : typeId
														},
														cache : false,
														success : function(data) {

															var reType = data.typeId;

															// $("#typeId").val(reType);
															// if (reType == 1)
															// {
															// $("#products")
															// .css("display",true);
															// }
															// if (reType == 2)
															// {
															// $("#gift")
															// .css("display",true);
															// }
															$(
																	"#products tr:not(:first)")
																	.remove();
															var results = data.pageResult.resultList;

															for (i = 0; i < results.length; i++) {

																var product = results[i];

																var rowContent = "<tr>"
																		+ "<td><input name='proId' class='checkbox' type='checkbox' value='"
																		+ product.id
																		+ "'/></td>"
																		+ "<td>"
																		+ product.proCode
																		+ "</td>"
																		+ "<td>"
																		+ product.proName
																		+ "</td>"
																		+ "<td>"
																		+ product.typeName
																		+ "</td>"
																		+ "<td>"
																		+ product.grown
																		+ "</td>"
																		+ "<td>"
																		+ product.total
																		+ "</td>"
																		+ "<td>"
																		+ product.minTotal
																		+ "</td>";
																rowContent = rowContent
																		+ "<td><button name='editBtn' objId='"
																		+ product.id
																		+ "' class='btn btn-sm-block'>修改</button></td>"
																		+ "</tr>";
																$("#products")
																		.append(
																				rowContent);

															}

														},
														error : function(html) {
															return;
														}
													});
										}
									});

					/**
					 * 全选当前页记录
					 */
					$("#products").delegate('#selectAll', 'click', function() {
						var isSelectAll = $(this).prop('checked');
						$("input[name='proId']").each(function() {
							$(this).prop("checked", isSelectAll);
						});
					});

					/**
					 * 修改按钮事件
					 */

					$("#products").delegate('[name="editBtn"]', 'click',
							function() {
								var proId = $(this).attr("objId");
								var typeId = $("#typeId").val()
								var editUrl = "show.do";
								$.ajax({

									url : editUrl,
									dataType : 'json',
									data : {
										id : proId,
										retype : typeId
									},
									cache : false,

									success : function(data) {
										alert("ajax data=" + data.id);
										loadStock(data);
									},
									error : function(html) {
										// alert(html);
										alert("Load Data failly");
										refresh();
									}
								});

							});

					$("#products").delegate('[name="viewResource"]', 'click',
							function() {
								var proId = $(this).attr("objId");
								// alert("proId="+proId);
								var editUrl = "showResouce.do";
								$.ajax({

									url : editUrl,
									dataType : 'json',
									data : {
										id : proId
									},
									cache : false,

									success : function(data) {

										loadResource(data);
									},
									error : function(html) {
										// alert(html);
										alert("Load Data failly");
										refresh();
									}
								});

							});

					function queryStock(reType, productName) {
						alert("productName=" + productName);
						alert("reType=" + reType);

						var queryUrl = "queryByPage.do";

						$
								.ajax({
									url : queryUrl,
									dataType : 'json',
									data : {
										pageIndex : pageIndex,
										pageSize : pSize,
										retype : reType
									},
									cache : false,
									success : function(data) {

										var results = data.pageResult.resultList;
										var reType = data.typeId;
										var typeArr = data.reTypes;

										if (reType == 1) {
											$("#products tr:not(:first)")
													.remove();
											$("#tip").css("display", "none");
											$("#products").css("width", "100%");
											$("#products").css("display",
													"block");
											for (i = 0; i < results.length; i++) {
												var product = results[i];
												var rowContent = "<tr><td><input name='proId' class='checkbox' type='checkbox' value='"
														+ product.id
														+ "'/></td><td>"
														+ product.proCode
														+ "</td><td>"
														+ product.proName
														+ "</td><td>"
														+ product.typeName
														+ "</td><td>"
														+ product.grown
														+ "</td><td>"
														+ product.total
														+ "</td><td>"
														+ product.minTotal
														+ "</td>";
												rowContent = rowContent
														+ "<td><button name='editBtn' objId='"
														+ product.id
														+ "' class='btn btn-sm-block'>修改</button></td></tr>";
												$("#products").append(
														rowContent);
											}
										}
										if (reType == 2) {
											$("#gift").css("display", "block");
										}

									},
									error : function(html) {
										return;
									}
								});
					}

					function loadStock(product) {
						alert("1" + product.proName)
						$("#proId").val(product.id);
						$("#proCode").val(product.proCode);
						$("#proName").val(product.proName);
						$("#grown").val(product.grown);
						$("#factory").val(product.factory);
						$("#typeId").val(product.typeId);
						$("#unit").val(product.unit);
						$("#total").val(product.total);
						$("#minTotal").val(product.minTotal);
						$("#description").val(product.description);
						$("#productDialog").modal("show");
					}

					function vaildate(form) {
						return true;
					}

					function refresh() {
						window.location.reload();
					}
				});
